﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using SMSPlatform.Gateway;
using System;
using System.Collections.ObjectModel;

namespace SMSPlatform
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1
    {
        private List<string> ports = new List<string>();
        private List<int> baudRates = new List<int>();
        private SMSGateway gateway;


        public Window1()
        {

            InitializeComponent();
            PopulatePorts();
            PopulateBaudRates();

        }

        private void PopulateBaudRates()
        {
            baudRates = new List<int>(new int[] { 110, 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200, 230400, 460800, 921600 });

            foreach (var item in baudRates)
            {
                baudRatesCombo.Items.Add(new ComboBoxItem { Content = item });
            }
        }

        private void PopulatePorts()
        {
            ports = new List<string>(System.IO.Ports.SerialPort.GetPortNames());

            foreach (var item in ports)
            {
                portCombo.Items.Add(new ComboBoxItem { Content = item });
            }
        }

        private void connect_Click(object sender, RoutedEventArgs e)
        {
            gateway = new SMSGateway(
                (portCombo.SelectedValue as ComboBoxItem).Content as string,
                (int)(baudRatesCombo.SelectedValue as ComboBoxItem).Content);

            gateway.StatusChanged += gateway_StatusChanged;
            gateway.DataReceived += (gateway_DataReceived);

            gateway.Connect();
        }

        void gateway_DataReceived(string data)
        {
            Dispatcher.Invoke(new Action(() => dataStack.Children.Add(new TextBlock
                                                                               {
                                                                                   Text = DateTime.Now + "\n" + data,
                                                                                   TextWrapping = TextWrapping.Wrap
                                                                               })));
        }

        void gateway_StatusChanged(bool oldValue, bool newValue)
        {
            if (newValue)
            {
                statusLabel.Content = "Status: Connected!";
            }
            else
            {
                statusLabel.Content = "Status: Disconnected!";
            }
        }

        private void at_Click(object sender, RoutedEventArgs e)
        {
            gateway.ExecuteATCommand("AT");
        }

        private void get_Click(object sender, RoutedEventArgs e)
        {
            gateway.ExecuteATCommand("AT+CMGF=1");
            gateway.ExecuteATCommand("AT+CMGL=\"ALL\"");
        }

        private void send_Click(object sender, RoutedEventArgs e)
        {
            gateway.SendSMS(numberText.Text, messageText.Text);
        }
    }
}
